package org.gitos.cloud.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;

public class AuthCodeKit {
	// 验证码图片宽度
	private static int imgWidth = 200;
	// 验证码图片高度
	private static int imgHeight = 80;
	// 验证码高度
	private static int fontHeight = 56;

	private static Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
		Random random = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}

	private static char getSingleNumberChar() {
		Random random = new Random();
		int numberResult = random.nextInt(10);
		int ret = numberResult + 48;
		return (char) ret;
	}

	private static char getLowerOrUpperChar(int upper) {
		Random random = new Random();
		int numberResult = random.nextInt(26);
		int ret = 0;
		if (upper == 0) {// 小写
			ret = numberResult + 97;
		} else if (upper == 1) {// 大写
			ret = numberResult + 65;
		}
		return (char) ret;
	}

	/** 生成指定长度只包含数字的验证码 */
	public static String generateAuthCodeOfNum(int length) {
		String NUM[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(NUM[random.nextInt(10)]);
		}
		return sb.toString();
	}

	/** 生成指定长度并包含数字和字母的验证码 */
	public static String generateAuthCode(int length) {
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int wordType = random.nextInt(3);
			char retWord = 0;
			switch (wordType) {
			case 0:
				retWord = getSingleNumberChar();
				break;
			case 1:
				retWord = getLowerOrUpperChar(0);
				break;
			case 2:
				retWord = getLowerOrUpperChar(1);
				break;
			}
			sb.append(retWord);
		}
		return sb.toString();
	}

	/** 根据验证码生成图片 */
	public static BufferedImage generateAuthCodeImage(String authCode) {
		int codeLength = authCode.length();
		int singleCharWidth = imgWidth / (codeLength + 1);
		Random random = new Random();
		BufferedImage image = new BufferedImage(imgWidth, imgHeight,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, imgWidth, imgHeight);

		// 设定字体
		g.setFont(new Font("Times New Roman", Font.PLAIN + Font.ITALIC,
				fontHeight));

		// 画边框
		g.setColor(new Color(55, 55, 12));
		g.drawRect(0, 0, imgWidth - 1, imgHeight - 1);

		// 随机产生30条干扰线，使图象中的认证码不易被其它程序探测到
		for (int i = 0; i < 30; i++) {
			int x = random.nextInt(imgWidth);
			int y = random.nextInt(imgHeight);
			int xl = random.nextInt(12);
			int yl = random.nextInt(12);
			g.setColor(getRandColor(160, 200));
			g.drawLine(x, y, x + xl, y + yl);
		}

		int red = 0, green = 0, blue = 0;
		for (int i = 0; i < codeLength; i++) {
			red = random.nextInt(255);
			green = random.nextInt(255);
			blue = random.nextInt(255);
			g.setColor(new Color(red, green, blue));
			g.drawString(String.valueOf(authCode.charAt(i)), i
					* singleCharWidth + singleCharWidth / 2,
					(imgHeight - fontHeight) / 2);
		}
		// 图象生效
		g.dispose();
		return image;
	}
}
